package model.admin;

import java.sql.ResultSet;
import java.util.ArrayList;

import myutil.Connect;

import vo.*;
import vo.mixed.CourseListVo;

public class ModifyCoursesMo {

	public void updateCourseDetails(int course_id,String course_name, String course_code,int credit, int duration, int lab,int theory,int department_id,int start_year,String status,String type) {
		String query = "update course_details set course_name='"+course_name+"' , course_code='"+course_code+"' , credit="+credit+" , duration="+duration+" , lab="+lab+", theory="+theory+" , start_year="+start_year+" ,status='"+status+"' where course_id="+course_id;
		
		System.out.println("Query is \t " + query + "\n");
		Connect c = new Connect();
		Boolean result;
		try {
			c.dml(query);
			c.logger(type, "Update");
			result = true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			result = false;
		}
		
	}

	public ArrayList<CourseListVo> getSingleCourseList(int course_id){
		ArrayList<CourseListVo> course = new ArrayList<CourseListVo>();
		String query="";
		query="select * from course_details where status='Active' and course_id="+course_id;
		System.out.println(query);
		Connect c= new Connect();
		try {
			ResultSet rs= c.getResult(query);
			while(rs.next()){
			CourseListVo vo = new CourseListVo();
			vo.setCourse_id(course_id);
			vo.setCourse_name(rs.getString("course_name"));
			vo.setDuration(rs.getInt("duration"));
			vo.setCredit(rs.getInt("credit"));
			vo.setLab(rs.getInt("lab"));
			vo.setTheory(rs.getInt("theory"));
			vo.setStart_year(rs.getInt("start_year"));
			vo.setDepartment_id(rs.getInt("department_id"));
			vo.setCourse_code(rs.getString("course_code"));
			vo.setStatus(rs.getString("status"));
			course.add(vo);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return course;
	}
	
	
	public Boolean deleteCourseDetails(int course_id,String type) {

		String query = "";
		query = "update course_details set status='Inactive' where course_id="
				+ course_id + ";";

		System.out.println(query);
		Connect c = new Connect();
		try {
			c.dml(query);
			c.logger(type, "Update");
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}

	public Boolean addCourseDetails(CourseListVo CourseDetails, int batch_id,String type) {
		// Adds new course to the courses list
		String query1 = "";
		if (batch_id != 0)
			query1 = "INSERT INTO degree_department(department_id, degree_id, batch_id, start_year, description,status) values "
					+ "((select department_id from course_details where course_name ='"
					+ CourseDetails.getCourse_name()
					+ "'), "
					+ "(select degree_id from degree_management where batch_id="
					+ batch_id
					+ "),"
					+ " "
					+ batch_id
					+ ","
					+ CourseDetails.getStart_year()
					+ ", "
					+ CourseDetails.getDescription() + ", 1); ";
		else
			query1 = "INSERT INTO course_details (course_name, duration, credit, lab, theory, start_year, department_id, course_code, status) VALUES "
					+ "('"+ CourseDetails.getCourse_name()+ "', "+ CourseDetails.getDuration()+ ", "+ CourseDetails.getCredit()+ ", "
					+ CourseDetails.getLab()+ ", "+ CourseDetails.getTheory()+ ", '"+ CourseDetails.getStart_year()+ "', "
					+ CourseDetails.getDepartment_id()+ ", '"
					+ CourseDetails.getCourse_code() + "', 'Active');";
		System.out.println(query1);
		Connect c = new Connect();
		try {
			c.dml(query1);
			c.logger(type, "Insert");
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}

	public boolean removeCourseFromBatch(int course_id, int batch_id) {
		String query = "";
		query = "select c.course_name," + " c.course_code," + " c.course_id , "
				+ " dd.department_name, " + " dd.department_id"
				+ " from degree_department d, " + " course_details c, "
				+ " department_details dd "
				+ "where d.department_id	=	dd.department_id "
				+ "and c.department_id	=	d.department_id "
				+ "and d.batch_id		=	" + batch_id + ";";
		return true;
	}

	public boolean deleteCourseProfessor(int course_id, int professor_id,String type) {
		String query = "update course_professor set professor_id_1=null where course_id="
				+ course_id + " and professor_id_1=" + professor_id + ";";
		String query1 = "update course_professor set professor_id_2=null where course_id="
				+ course_id + " and professor_id_2=" + professor_id + ";";
		String query2 = "update course_professor set professor_id_3=null where course_id="
				+ course_id + " and professor_id_3=" + professor_id + ";";
		String query3 = "update course_professor set professor_id_4=null where course_id="
				+ course_id + " and professor_id_4=" + professor_id + ";";

		Connect c = new Connect();
		System.out.println(query);
		try {
			c.dml(query);
			c.dml(query1);
			c.dml(query2);
			c.dml(query3);
			c.logger(type, "Update");
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}

	}

	public Boolean CourseProfessorCourseDetails() {
		String query = "";
		return false;
	}

	public Boolean addDepartmentCourseDetails(int department_id, String course_name,String type) {
		String query = "";
		query = "UPDATE course_details " + " SET department_id="
				+ department_id + " " + "WHERE course_id=(select course_id from course_details where course_id=" + course_name + ");";

		Connect c = new Connect();
		System.out.println(query);
		try {
			c.dml(query);
			c.logger(type, "Update");
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}

	}
}
